package com.virgo.demo.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.virgo.common.entity.ListDataVo;
import com.virgo.common.exception.IApplicationException;
import com.virgo.common.utils.ICommonUtils;
import com.virgo.demo.market.converter.AdvPositionConverter;
import com.virgo.demo.market.domain.AdvDo;
import com.virgo.demo.market.domain.AdvPositionDo;
import com.virgo.demo.market.domain.bo.AdvPositionBo;
import com.virgo.demo.market.domain.vo.AdvPositionVo;
import com.virgo.demo.market.mapper.AdvMapper;
import com.virgo.demo.market.mapper.AdvPositionMapper;
import com.virgo.demo.market.service.AdvPositionService;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AdvPositionServiceImpl extends ServiceImpl<AdvPositionMapper, AdvPositionDo> implements AdvPositionService {

	private final AdvPositionMapper advPositionMapper;

	private final AdvPositionConverter advPositionConverter;

	private final AdvMapper advMapper;

	/**
	 * 根据广告位标识查询
	 * @param positionCode String
	 * @return AdvPositionDo
	 */
	@Override
	public AdvPositionDo loadByPositionCode(String positionCode) {
		LambdaQueryWrapper<AdvPositionDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AdvPositionDo::getCode,positionCode);
		queryWrapper.last(" limit 1 ");
		return getOne(queryWrapper);
	}

	/**
	 * 编辑广告位
	 * @param advPositionBo AdvPositionBo
	 */
	@Override
	public void saveAdvPosition(AdvPositionBo advPositionBo) {
		AdvPositionDo exist = loadByPositionCode(advPositionBo.getCode());
		AdvPositionDo advPositionDo = new AdvPositionDo();
		BeanUtils.copyProperties(advPositionBo,advPositionDo);
		if (!ObjectUtils.isEmpty(exist) && !exist.getId().equals(advPositionDo.getId())) {
			throw new IApplicationException("广告位-" + advPositionDo.getCode() + "已存在");
		}
		if (ObjectUtils.isEmpty(advPositionDo.getId()) || advPositionDo.getId().compareTo(0L) < 1) {
			save(advPositionDo);
			return;
		}
		updateById(advPositionDo);
	}

	/**
	 * 删除广告位
	 * @param codes String
	 */
	@Override
	public void deleteAdvPosition(String codes) {
        List<String> positionCodeList = Arrays.asList(codes.split(","));
		LambdaQueryWrapper<AdvDo> advDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
		advDoLambdaQueryWrapper.in(AdvDo::getPositionCode,positionCodeList);
		long advNum = advMapper.selectCount(advDoLambdaQueryWrapper);
		if (advNum > 0) {
			throw new IApplicationException("存在广告，不能删除");
		}
        LambdaQueryWrapper<AdvPositionDo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(AdvPositionDo::getCode, positionCodeList);
        remove(lambdaQueryWrapper);
	}

	/**
	 * 广告位列表
	 * @param request HttpServletRequest
	 * @return ListDataVo<AdvPositionVo>
	 */
	@Override
	public ListDataVo<AdvPositionVo> findList(HttpServletRequest request) {
		long currentPage = IRequestUtils.getLong(request,"page");
		long pageSize = IRequestUtils.getLong(request,"pageSize");
		currentPage = Math.max(currentPage,1);
		pageSize = Math.max(pageSize,10);

		LambdaQueryWrapper<AdvPositionDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(AdvPositionDo::getId);

		String name = IRequestUtils.getString(request,"name","");
		if (!ObjectUtils.isEmpty(name)) {
			queryWrapper.like(AdvPositionDo::getName,name);
		}

		Page<AdvPositionDo> page = new Page<>(currentPage, pageSize);
		IPage<AdvPositionDo> iPage = advPositionMapper.selectPage(page, queryWrapper);
		return new ListDataVo<>(iPage.getTotal(),iPage.getPages(),advPositionConverter.convert(iPage.getRecords(), AdvPositionVo.class));
	}
}